package com.vvlive.caibao.smartcity.view;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.utils.ScreenUtil;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/7/4.
 */

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@SuppressLint("NewApi")
public class SlideSwitcherView extends LinearLayout implements OnClickListener {

    /**
     * 默认屏幕的宽度
     */
    private final int DEFAULT_SCREEN_WIDTH = 540;

    /**
     * 默认布局宽度
     */
    private final int DEFAULT_LAYOUT_WIDTH = 260;
    /**
     * 默认布局高度
     */
    private final int DEFAULT_LAYOUT_HEIGHT = 50;
    /***
     * 默认的滑块移动时间
     */
    private final int DEFAULT_MOVE_DURATION = 200;
    /**
     * 默认的标签字体大小
     */
    private final int DEFAULT_TEXT_SIZE = 20;

    /**
     * 默认滑块的宽度
     */
    private final int DEFAULT_SLIDER_WIDTH = DEFAULT_LAYOUT_WIDTH / 2;
    /**
     * 默认的滑块高度
     */
    private final int DEFAULT_SLIDER_HEIGHT = DEFAULT_LAYOUT_HEIGHT;

    /**
     * 默认获得焦点时标签的颜色
     */
    private final int DEFAULT_FOCUS_TEXT_COLOR = Color.WHITE;
    /**
     * 没有获得焦点时标签的颜色
     */
    private final int DEFAULT_NORMAL_TEXT_COLOR = Color.parseColor("#595959");
    /**
     * 默认的滑块颜色
     */
    private final int DEFAULT_SLIDER_COLOR = Color.parseColor("#02C96E");

    private View layout;                // 总布局
    private ImageView slider;                // 滑块
    protected LinearLayout labelsContainer;       // 标签的父布局
    private TextView firstLabel;            // 第一个标签
    private TextView secondLabel;           // 第二个标签

    private OnSlideSwitchListener onSlideSwitchListener;    // 点击标签的监听

//    private float percent;      // 当前屏幕的宽度和默认屏幕的宽度的百分比

    private int layoutWidth;    // 布局宽度
    private int layoutHeight;   // 布局高度
    private int sliderHeight;   // 滑块的高度
    private int sliderWidth;    // 滑块的宽度
    private int moveDistance;   // 滑块的移动距离
    private int moveDuration;   // 滑块的移动时间

    private int focusTextColor; // 获得焦点时的文本颜色
    private int normalTextColor;// 没有焦点时的文本颜色

    private int textSize;       // 标签的字体大小

    private ScreenUtil screenUtil;  // 获得屏幕的各项参数

    public SlideSwitcherView(Context context) {
        this(context, null);
    }

    public SlideSwitcherView(Context context, AttributeSet attrs) {
        super(context, attrs);
        screenUtil = new ScreenUtil(context);
        initData();
        initLayout();
    }

    /**
     * 初始化数据,适配屏幕
     */
    private void initData() {

//        percent         = screenUtil.getPercent();

        // 按照屏幕的比例进行缩放
        layoutWidth = (int) screenUtil.getScaledSize(DEFAULT_LAYOUT_WIDTH);
        layoutHeight = (int) screenUtil.getScaledSize(DEFAULT_LAYOUT_HEIGHT);
        sliderWidth = (int) screenUtil.getScaledSize(DEFAULT_SLIDER_WIDTH);
        sliderHeight = (int) screenUtil.getScaledSize(DEFAULT_SLIDER_HEIGHT);

        moveDistance = layoutWidth / 2;
        moveDuration = DEFAULT_MOVE_DURATION;
        focusTextColor = DEFAULT_FOCUS_TEXT_COLOR;
        normalTextColor = DEFAULT_NORMAL_TEXT_COLOR;

        textSize = (int) screenUtil.getTargetTextSize(DEFAULT_TEXT_SIZE);
    }

    /**
     * 初始化布局
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void initLayout() {

        layout = LayoutInflater.from(getContext()).inflate(R.layout.view_slide_switcher, null, false);
        layout.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, layoutHeight));

        /**
         * 找到滑块，并设置滑块的颜色。因为在{@link TripleSlideSwitcherView}对滑块的颜色
         * 进行了设定，直接作用在drawable文件上，所以在此类中重新读取的时候，颜色会发生改变，
         * 所以需要重新设置。
         */
        slider = (ImageView) layout.findViewById(R.id.ss_slider);
        setSliderColor(DEFAULT_SLIDER_COLOR);

        labelsContainer = (LinearLayout) layout.findViewById(R.id.labels_container);
        firstLabel = (TextView) layout.findViewById(R.id.ss_left_label);
        secondLabel = (TextView) layout.findViewById(R.id.ss_right_label);
        firstLabel.setOnClickListener(this);
        secondLabel.setOnClickListener(this);

        // 默认第一个标签获得焦点
        firstLabel.setTextColor(DEFAULT_FOCUS_TEXT_COLOR);

        this.addView(layout);

        measureLayout();

    }

    private void measureLayout() {

        LayoutParams labelParams = new LayoutParams(sliderWidth, sliderHeight);
        for (int i = 0; i < labelsContainer.getChildCount(); i++) {

            TextView textView = (TextView) labelsContainer.getChildAt(i);
            textView.setTextSize(textSize);
            textView.setLayoutParams(labelParams);
        }

        setLayoutWidth(layoutWidth);
        setLayoutHeight(layoutHeight);
        setRadius(layoutHeight / 2);
    }

    public void setOnSlideSwitchListener(OnSlideSwitchListener onSlideSwitchListener) {
        this.onSlideSwitchListener = onSlideSwitchListener;
    }

    @Override
    public void onClick(View v) {
        int position = getChildIndex(v, labelsContainer);
        int distance = moveDistance * position;
        slide(distance);
        for (int i = 0; i < labelsContainer.getChildCount(); i++) {
            ((TextView) labelsContainer.getChildAt(i)).setTextColor(normalTextColor);
        }
        ((TextView) v).setTextColor(focusTextColor);
        if (onSlideSwitchListener != null) {
            onSlideSwitchListener.onSlideSwitch(v, position);
        }
    }

    /**
     * 设置滑块的运动时间
     *
     * @param moveDuration
     */
    public void setMoveDuration(int moveDuration) {
        this.moveDuration = moveDuration;
    }

    /**
     * 设置标签获得焦点时的颜色
     *
     * @param focusTextColor
     */
    public void setFocusTextColor(int focusTextColor) {
        this.focusTextColor = focusTextColor;
        firstLabel.setTextColor(focusTextColor);
    }

    /**
     * 设置标签正常颜色
     *
     * @param normalTextColor
     */
    public void setNormalTextColor(int normalTextColor) {
        this.normalTextColor = normalTextColor;
        secondLabel.setTextColor(normalTextColor);
    }

    /**
     * 设置第一个标签文本
     *
     * @param leftLabelText
     */
    public void setFirstLabel(String leftLabelText) {
        firstLabel.setText(leftLabelText);
    }

    /**
     * 设置第二个标签文本
     *
     * @param rightLabelText
     */
    public void setSecondLabel(String rightLabelText) {
        secondLabel.setText(rightLabelText);
    }

    /**
     * 设置滑块颜色
     *
     * @param color
     */
    public void setSliderColor(int color) {
        ((GradientDrawable) slider.getDrawable()).setColor(color);
    }

    /**
     * 设置滑块颜色
     *
     * @param colorText
     */
    public void setSliderColor(String colorText) {
        setSliderColor(Color.parseColor(colorText));
    }

    /**
     * 设置背景颜色
     *
     * @param color
     */
    public void setLayoutColor(int color) {
        ((GradientDrawable) layout.getBackground()).setColor(color);
    }

    /**
     * 设置背景颜色
     *
     * @param colorText
     */
    public void setLayoutColor(String colorText) {
        setLayoutColor(Color.parseColor(colorText));
    }

    /**
     * 设置宽度
     *
     * @param width
     */
    public void setLayoutWidth(int width) {
        layoutWidth = width;
        moveDistance = width / labelsContainer.getChildCount();
        layout.setMinimumWidth(width);
        labelsContainer.setLayoutParams(new FrameLayout.LayoutParams(width, layoutHeight));
        slider.setLayoutParams(new FrameLayout.LayoutParams(moveDistance, layoutHeight));
    }

    /**
     * 设置高度
     *
     * @param height
     */
    public void setLayoutHeight(int height) {
        layoutHeight = height;
        layout.setMinimumHeight(height);
        labelsContainer.setLayoutParams(new FrameLayout.LayoutParams(layoutWidth, height));
        slider.setLayoutParams(new FrameLayout.LayoutParams(moveDistance, height));
        setRadius(height / 2);
    }

    /**
     * 设置边缘角度
     *
     * @param radius
     */
    public void setRadius(float radius) {
        ((GradientDrawable) slider.getDrawable()).setCornerRadius(radius);
        ((GradientDrawable) layout.getBackground()).setCornerRadius(radius);
    }

    /**
     * 添加一个标签
     *
     * @param text 标签文本
     */
    public void addLabel(String text) {
        TextView label = createLabel(text);
        labelsContainer.addView(label);
        int count = labelsContainer.getChildCount();
        int width = layoutWidth / (count - 1) * count;
        setLayoutWidth(width);
    }

    /**
     * 创建一个label
     *
     * @param text
     * @return
     */
    private TextView createLabel(String text) {
        TextView label = new TextView(getContext());
        label.setText(text);
        label.setLayoutParams(firstLabel.getLayoutParams());
        label.setTextSize(firstLabel.getTextSize() / screenUtil.getDensity());
        label.setGravity(Gravity.CENTER);
        label.setTextColor(normalTextColor);
        label.setOnClickListener(this);
        return label;

    }

    /**
     * 得到一个子控件在父布局中的位置
     *
     * @param child
     * @param parent
     * @return
     */
    private int getChildIndex(View child, ViewGroup parent) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            if (child.equals(parent.getChildAt(i)))
                return i;
        }
        return -1;
    }

    private void slide(int distance) {
        slider.animate().translationX(distance).setDuration(moveDuration).start();
    }

    public interface OnSlideSwitchListener {
        void onSlideSwitch(View v, int position);
    }

}
